<template>
  <div>
    <div class="buttons">
      <div class="buttonGroup1">
        <el-input style="width: 200px" placeholder="查询用户名" v-model="username"></el-input>
        <el-input style="width: 200px; margin: 0 5px" placeholder="查询姓名" v-model="name"></el-input>
        <el-button type="primary" @click="load(1)">查询</el-button>
        <el-button type="info" @click="reset">重置</el-button>
      </div>
      <div class="buttonGroup2">
        <el-button type="primary" @click="addUserVisible = true">新增</el-button>
        <el-button type="danger" plain @click="delBatch">批量删除</el-button>
      </div>
    </div>
    <el-table :data="tableData" stripe :header-cell-style="{backgroundColor: 'aliceblue', color: '#666'}"
              style="margin-top: 10px" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="id" label="序号" width="50" align="center"></el-table-column>
      <el-table-column prop="username" label="账号" align="center"></el-table-column>
      <el-table-column prop="name" label="用户名" align="center"></el-table-column>
      <el-table-column prop="role" label="权限" align="center"></el-table-column>
      <el-table-column prop="phone" label="电话" align="center"></el-table-column>
      <el-table-column prop="address" label="地址" align="center"></el-table-column>
      <el-table-column prop="salt" label="验证码" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template v-slot="scope">
          <!--          <el-button size="mini" type="primary" plain @click="">编辑</el-button>-->
          <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :hide-on-single-page="hideValue"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="添加用户" :visible.sync="addUserVisible">
      <el-form ref="addUserTable" inline>
        <el-form-item>
          <el-select v-model="value" placeholder="请选择">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="num">
          <el-input v-model="userNum" placeholder="请输入数量"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addUserVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/urils/request";

export default {
  name: "user",
  data() {
    return {
      hideValue: false,
      username: '',
      name: '',
      tableData: [],
      pageNum: 1,
      pageSize: 11,
      total: 0,

      addUserVisible: false,
      options: [{
        value: 2,
        label: '地市账号'
      }, {
        value: 3,
        label: '高校账号'
      }, {
        value: 4,
        label: '中小学账号'
      }, {
        value: 5,
        label: '专家账号'
      }],
      value: '',
      userNum: '',
      ids: []
    }
  },
  methods: {
    reset() {
      this.name = ''
      this.username = ''
      this.load()
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      request.get('/user/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          username: this.username,
          name: this.name
        }
      }).then(res => {
        // console.log(res.data)
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    addUser() {
      request.get("/user/add", {
        params: {
          userNum: this.userNum,
          value: this.value
        }
      }).then(res => {
        if (res.code === '200') {
          this.$message.success("添加成功")
          this.addUserVisible = false
          location.reload()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    del(id) {
      this.$confirm('是否确认删除该数据', '确认删除', {type: "warning"}).then(response => {
        request.delete('/user/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            // location.reload()
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    delBatch() {
      if (!this.ids.length) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('是否确认批量删除该数据', '确认删除', {type: "warning"}).then(response => {
        request.delete('/user/delete/batch', {data: this.ids}).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
  },
  created() {
    this.load()
    if (this.total === 0) {
      this.hideValue = true
    }

    const role = JSON.parse(localStorage.getItem('user')).role
    // console.log(role)
    if (role === '管理员') {
      this.options.push({value: 1, label: '省厅账号'})
    }
  }
}
</script>

<style scoped>
.buttons {
  width: 100%;
  display: flex;
}

.buttonGroup1 {
  flex: 1;
}

.buttonGroup2 {
  margin-right: 5px;
  flex: 1;
  display: flex;
  justify-content: flex-end;
}
</style>